import request from "@/utils/request";
import type { CategoryList } from "./models/categoryModels";

// 统一定义接口的请求地址
enum Api {
  // 获取
  getCategory1ListUrl = `/admin/product/getCategory1`,
  getCategory2ListUrl = `/admin/product/getCategory2`,
  getCategory3ListUrl = `/admin/product/getCategory3`,
  // 添加
  addCategory1Url = `/admin/product/saveCategory1`,
  addCategory2Url = `/admin/product/saveCategory2`,
  addCategory3Url = `/admin/product/saveCategory3`,
  // 更新
  updateCategory1Url = `/admin/product/updateeCategory1`,
  updateCategory2Url = `/admin/product/updateeCategory2`,
  updateCategory3Url = `/admin/product/updateeCategory3`,
  // 删除
  removeCategory1Url = `/admin/product/removeCategory1`,
  removeCategory2Url = `/admin/product/removeCategory2`,
  removeCategory3Url = `/admin/product/removeCategory3`,
}

/**
 * 删除一级分类
 * @param id 分类id
 * @returns null
 */
export const removeCategory1Api = (id: number) => {
  return request.delete<any, null>(Api.removeCategory1Url + `/${id}`);
};

/**
 * 删除二级分类
 * @param id 分类id
 * @returns null
 */
export const removeCategory2Api = (id: number) => {
  return request.delete<any, null>(Api.removeCategory2Url + `/${id}`);
};

/**
 * 删除三级分类
 * @param id 分类id
 * @returns null
 */
export const removeCategory3Api = (id: number) => {
  return request.delete<any, null>(Api.removeCategory3Url + `/${id}`);
};

/**
 * 添加一级分类
 * @param name 分类名称
 * @returns null
 */
export const addCategory1Api = (name: string) => {
  return request.post<any, null>(Api.addCategory1Url, { name });
};

/**
 * 添加二级分类
 * @param name 分类名称
 * @param category1Id 一级分类id
 * @returns null
 */
export const addCategory2Api = (name: string, category1Id: number) => {
  return request.post<any, null>(Api.addCategory2Url, { name, category1Id });
};

/**
 * 添加三级分类
 * @param name 分类名称
 * @param category2Id 二级分类id
 * @returns null
 */
export const addCategory3Api = (name: string, category2Id: number) => {
  return request.post<any, null>(Api.addCategory3Url, { name, category2Id });
};

/**
 * 更新一级分类
 * @param id 分类id
 * @param name 分类名称
 * @returns null
 */
export const updateCategory1Api = (id: number, name: string) => {
  return request.put<any, null>(Api.updateCategory1Url, { id, name });
};

/**
 * 更新二级分类
 * @param id 二级分类id
 * @param name 分类名称
 * @param category1Id 一级分类id
 * @returns null
 */
export const updateCategory2Api = (
  id: number,
  name: string,
  category1Id: number
) => {
  return request.put<any, null>(Api.updateCategory2Url, {
    id,
    name,
    category1Id,
  });
};

/**
 * 更新三级分类
 * @param id 三级分类id
 * @param name 分类名称
 * @param category2Id 二级分类id
 * @returns null
 */
export const updateCategory3Api = (
  id: number,
  name: string,
  category2Id: number
) => {
  return request.put<any, null>(Api.updateCategory3Url, {
    id,
    name,
    category2Id,
  });
};

/**
 * 获取一级分类列表
 * @returns CategoryList
 */
export const getCategory1ListApi = () => {
  return request.get<any, CategoryList>(Api.getCategory1ListUrl);
};

/**
 * 获取二级分类列表
 * @param category1Id 一级分类id
 * @returns CategoryList
 */
export const getCategory2ListApi = (category1Id: number) => {
  return request.get<any, CategoryList>(
    Api.getCategory2ListUrl + `/${category1Id}`
  );
};

/**
 * 获取三级分类列表
 * @param category2Id 二级分类id
 * @returns CategoryList
 */
export const getCategory3ListApi = (category2Id: number) => {
  return request.get<any, CategoryList>(
    Api.getCategory3ListUrl + `/${category2Id}`
  );
};
